//
//  MyTravelsViewController.m
//  哎游
//
//  Created by 康守帅 on 15-1-14.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MyTravelsViewController.h"
#import "TravelsListTableViewCell.h"
#import "TravelsDataBase.h"
#import "macro.h"
#import "UIImageView+WebCache.h"
#import "TravelsDetailViewController.h"
#import "Singleton.h"

@interface MyTravelsViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation MyTravelsViewController

- (void)dealloc
{
    [_tableArray release];
    [_tableView release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    [button setBackgroundImage:[UIImage imageNamed:@"return.png"] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:self action:@selector(returnBack) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 45, 100)];
    label.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:20];
    label.text = @"游记收藏";
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    [self.tableArray removeAllObjects];
    
    [button release];
    [label release];
    [self selectTravels];
    [self createTableView];
}

- (void)selectTravels
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [self.tableArray removeAllObjects];
    NSString *str = [user objectForKey:@"state"];
    if ([str isEqualToString:@"sina"]) {
        [_tableArray addObjectsFromArray:[TravelsDataBase selectUserTravelsWithUser:[user objectForKey:@"userSina"]]];
    }else {
        [_tableArray addObjectsFromArray:[TravelsDataBase selectUserTravelsWithUser:[user objectForKey:@"userName"]]];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [self selectTravels];
    [self.tableView reloadData];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[TravelsListTableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    TravelsListModel *model = [_tableArray objectAtIndex:indexPath.row];
    [cell.backImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_image_default] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    cell.title.text = model.name;
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[model.first_day intValue]];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:confromTimesp];
    NSDate *localeDate = [confromTimesp  dateByAddingTimeInterval: interval];
    
    NSInteger timeOffset = [zone secondsFromGMTForDate:localeDate];
    
    NSDate *newDate = [localeDate dateByAddingTimeInterval:timeOffset];
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy.MM.dd"];
    NSString *dateStr = [dateFormat stringFromDate: newDate];
    cell.date.text = dateStr;
    cell.dateNum.text = [model.day_count stringValue];
    cell.visits.text = [model.view_count stringValue];
    cell.address.text = model.popular_place_str;
    
    [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:model.userImage] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    
    TravelsDetailViewController *travels = [[TravelsDetailViewController alloc] init];
    travels.imageUser = model.userImage;//给detil传值
    
    cell.userName.text = model.userName;
    
    [dateFormat release];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 230;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TravelsListModel *model = [_tableArray objectAtIndex:indexPath.row];
    TravelsDetailViewController *travels = [[TravelsDetailViewController alloc] init];
    
    travels.ID = model.ID;
    travels.name = model.name;
    travels.first_day = model.first_day;
    travels.day_count = model.day_count;
    travels.travelsListModel = model;
    
    [self.navigationController pushViewController:travels animated:YES];
    [travels release];
}

- (void) returnBack
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
